package com.pandacc.dao.fiction;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.context.WebApplicationContext;

import com.pandacc.common.utils.ServiceDaoUtils;
import com.pandacc.dao.SqlLoadedFromPropertiesDAO;
import com.pandacc.dao.fiction.rowmapper.FictionChaptersDetailsBeanRowMapper;
import com.pandacc.pojo.fiction.FictionChaptersDetailsBean;

/**
 * 处理小说章节访问数据的DAO层
 * @author Administrator
 *
 */
@Repository("chapterDAO")
@Scope("prototype")
@Lazy
public class FictionChaptersDetailsDAO extends SqlLoadedFromPropertiesDAO{
	private Log logger = LogFactory.getLog(FictionChaptersDetailsDAO.class);
	private static int i = 0;
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	public FictionChaptersDetailsDAO(){
		i++;
		logger.info("init FictionChaptersDetailsDAO bean @ " + i);
	}

	public List<String> retrieveAllChapters(WebApplicationContext context, int factionId) {
		return null;
	}

	/**
	 * 获取单章节信息
	 * @param context
	 * @param fictionId
	 * @param chapterId
	 * @return
	 */
	public FictionChaptersDetailsBean retrieveChapter(WebApplicationContext context, int fictionId, int chapterId) {
		String sql;
		JdbcTemplate template;
		try {
			 sql = loadSql("retrieveChapter");
			logger.info(String.format("Retrieve one chapter in a fiction with sql: %s.", sql));
			template = ServiceDaoUtils.getJdbcTemplate(context, "dataSource");
			List<FictionChaptersDetailsBean> chapterList = template.query(sql, new Integer[]{fictionId, chapterId},new FictionChaptersDetailsBeanRowMapper());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return null;
	}
	
	/**
	 * 获取单章节信息
	 * @param context
	 * @param fictionId
	 * @param chapterId
	 * @return
	 */
	public FictionChaptersDetailsBean retrieveChapter(int fictionId, int chapterId) {
		String sql;
		try {
			sql = loadSql("retrieveChapter");
			logger.info(String.format("Retrieve one chapter in a fiction with sql: %s.", sql));
			List<FictionChaptersDetailsBean> chapterList = jdbcTemplate.query(sql, new Integer[]{fictionId, chapterId},new FictionChaptersDetailsBeanRowMapper());
			if (chapterList != null && chapterList.size() ==1) {
				logger.info(chapterList.get(0).toString());
				return chapterList.get(0);
			}else{
				logger.error("The current chapters fetched is not only one.");
				return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		try {
			System.out.println(new FictionChaptersDetailsDAO().loadSql("retrieveChapter"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
